package com.yyr.callmessage.core.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yyr.callmessage.core.ro.BaseRo;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;


public class ServletUtils {
	public static void responseToJson(HttpServletResponse response, BaseRo responseBean) {
		response.setContentType("application/json; charset=utf-8");  
        response.setCharacterEncoding("UTF-8");
		// 设置允许跨域访问的域，*表示支持所有的来源
		response.setHeader("Access-Control-Allow-Origin", "*");
		// 设置允许跨域访问的方法
		response.setHeader("Access-Control-Allow-Methods",
				"POST, GET, OPTIONS, DELETE");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        
        Gson gson = new GsonBuilder().create();
        
		try {
			String json = gson.toJson(responseBean);
			OutputStream out = response.getOutputStream();
			out.write(json.getBytes("UTF-8"));  
	        out.flush();
	        
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
